package com.wgchao.identityrightsmanagement.infrastructure.dao;

import com.wgchao.identityrightsmanagement.domain.resource.model.FunctionResources;
import com.wgchao.identityrightsmanagement.domain.resource.model.MenuResources;
import com.wgchao.identityrightsmanagement.domain.resource.repository.MenuRepository;
import com.wgchao.identityrightsmanagement.infrastructure.convert.FunctionConvert;
import com.wgchao.identityrightsmanagement.infrastructure.convert.MenuConvert;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectExistException;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysResourceFunction;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysResourceMenu;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysResourceFunctionRepository;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysResourceMenuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: MenuDao
 * @author: wgchao
 * @createTime: 2020/6/18 3:11 PM
 * @Description: TODO
 */
@Component
public class MenuDao implements MenuRepository {
    @Autowired
    private SysResourceMenuRepository sysResourceMenuRepository;
    @Autowired
    private SysResourceFunctionRepository sysResourceFunctionRepository;
    @Autowired
    private MenuConvert menuConvert;

    @Override
    public MenuResources findById(String s) {
        SysResourceMenu sysResourceMenu = sysResourceMenuRepository.findFirstByMenuMark(s);
        if(sysResourceMenu == null){
            return null;
        }
        return menuConvert.convert(sysResourceMenu);
    }

    @Override
    public void remove(MenuResources menuResources, String operator) {
        SysResourceMenu sysResourceMenu = sysResourceMenuRepository.findFirstByMenuMark(menuResources.getMenuMark());
        if(sysResourceMenu == null){
            throw new ObjectNotExistException("菜单不存在");
        }
        //删除菜单上的功能资源
        List<SysResourceFunction> sysResourceFunctions = sysResourceFunctionRepository.findAllByMenuMark(menuResources.getMenuMark());
        if(sysResourceFunctions!=null && !sysResourceFunctions.isEmpty()){
            sysResourceFunctionRepository.deleteAll(sysResourceFunctions);
        }
        //删除菜单资源
        sysResourceMenuRepository.delete(sysResourceMenu);
    }

    @Override
    public void add(MenuResources menuResources, String operator) {
        if(menuResources.getId().compareTo(0L) < 0){//新增
            SysResourceMenu sysResourceMenu = sysResourceMenuRepository.findFirstByMenuMark(menuResources.getMenuMark());
            if(sysResourceMenu != null){
                throw new ObjectExistException("菜单标识已被占用");
            }
            sysResourceMenu = menuConvert.convert(menuResources);
            sysResourceMenu.setId(null);
            sysResourceMenu.setCreateTime(new Date());
            sysResourceMenu.setCreator(operator);
            sysResourceMenu = sysResourceMenuRepository.save(sysResourceMenu);
        }else{
            SysResourceMenu sysResourceMenu_old = sysResourceMenuRepository.findFirstByMenuMark(menuResources.getMenuMark());
            if(sysResourceMenu_old == null){
                throw new ObjectNotExistException("菜单不存在");
            }
            SysResourceMenu sysResourceMenu = menuConvert.convert(menuResources);
            sysResourceMenu.setCreator(sysResourceMenu_old.getCreator());
            sysResourceMenu.setCreateTime(sysResourceMenu_old.getCreateTime());
            sysResourceMenu.setLastUpdateTime(new Date());
            sysResourceMenu.setLastUpdator(operator);
            sysResourceMenu = sysResourceMenuRepository.save(sysResourceMenu);

        }
    }

    @Override
    public void addAll(List<MenuResources> menuResources, String operator) {

    }

    @Override
    public void removeAll(List<MenuResources> menuResources, String operator) {

    }
}
